package com.jxnu.duewang.zmly.zouZhen.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Project Zmly
 * Author Duewang
 * Time 2016/12/9 0009
 */

public class DiscoveryEditorListView extends ListView {

    public DiscoveryEditorListView(Context context) {
        super(context);
    }

    public DiscoveryEditorListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DiscoveryEditorListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        ListAdapter listAdapter = getAdapter();
        int count = getCount();
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height=0;
        for (int i = 0; i < count; i++) {
            View listItem = listAdapter.getView(i, null, this);
//            int desiredWidth = View.MeasureSpec.makeMeasureSpec(getWidth(), View.MeasureSpec.AT_MOST);
//            int desiredHeight = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
            listItem.measure(0,0);
            params.height+=listItem.getMeasuredHeight();
//            Log.e("测试", String.valueOf(count)+"-"+i+":"+String.valueOf(params.height));
        }
        params.height=params.height+(getDividerHeight()*(count-1));
        setLayoutParams(params);
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}
